<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" label-position="left" :model="formData" label-width="90px" :rules="formRules">
        <el-form-item label="酒店" prop="hotel_id">
          <el-select :disabled="mode==='edit'" placeholder="请选择酒店" v-model="formData.hotel_id">
            <el-option v-for="(item,index) in shopList" :key="index" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="入住天数" prop="day">
          <el-input placeholder="请输入住宿天数" type="number" v-model.number="formData.day"/>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="lineHotelRelEdit">
import type {FormInstance} from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {apiLineHotelRelAdd, apiLineHotelRelEdit, apiLineHotelRelDetail, lineAllShop} from '@/api/line_hotel_rel'
import {timeFormat} from '@/utils/util'
import type {PropType} from 'vue'

const props = defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  },
  lineId: {
    type: String,
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

const shopList = ref([])

async function initShopList() {
  const resp = await lineAllShop()
  shopList.value = resp.map((item: any) => {
    return {value: item.id, label: item.hotel_name}
  }) || []
}


// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑线路关联酒店' : '新增线路关联酒店'
})

// 表单数据
const formData = reactive({
  id: '',
  hotel_id: '',
  isEdit: false,
  day: '',
})


// 表单验证
const formRules = reactive<any>({
  hotel_id: [
    {
      required: true,
      message: '请选择要绑定的酒店'
    },
  ],
  day: [
    {
      required: true,
      message: '请输入住宿天数'
    }
  ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await apiLineHotelRelDetail({
    id: row.id
  })
  setFormData(data)
}

function getPayload() {
  const { hotel_id, day} = formData
  const {lineId} = props
  const data = {id: lineId, hotel_id, day}
  if (mode.value === 'edit') {
    data.id = formData.id
  }
  return data
}

// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate()
  const data = getPayload()
  mode.value == 'edit'
      ? await apiLineHotelRelEdit(data)
      : await apiLineHotelRelAdd(data)
  popupRef.value?.close()
  emit('success')
}

//打开弹窗
const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
  initShopList()
}

// 关闭回调
const handleClose = () => {
  emit('close')
}


defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
